//
//  LWstatusesCell.swift
//  Microblog
//
//  Created by Roman on 17/1/9.
//  Copyright © 2017年 Roman. All rights reserved.
//

import UIKit
import SDWebImage
private let homeCellMargin :CGFloat = 8

//计算最大的显示宽度
private let MaxWidth: CGFloat = ScreenWidth - 2 * homeCellMargin

//每张图片的间距
private let PictureCellMargin: CGFloat = 2

//计算每张图片的固定宽度
private let itemWith: CGFloat = (MaxWidth - 2 * PictureCellMargin) / 3


class LWstatusesCell: UITableViewCell {
    //微博作者头像
    @IBOutlet weak var headIcon: UIImageView!
    
    //微博作者名称
    @IBOutlet weak var Label_name: UILabel!
    
    //微博作者等级
    @IBOutlet weak var rankView: UIImageView!
    
    //微博时间
    @IBOutlet weak var Label_time: UILabel!
  
    //微博来源
    @IBOutlet weak var Label_source: UILabel!
    
    //原创微博内容
    @IBOutlet weak var Label_content: UILabel!
    
    //转发微博内容
    @IBOutlet weak var lABEL_retweeteD: UILabel!
    //认证类型
    @IBOutlet weak var verifiedIcon: UIImageView!
    
  
    @IBOutlet weak var PictureView: LWPictureCollectionView!
    
    
      //高度的约束对象
    
    @IBOutlet weak var pictureHeightCons: NSLayoutConstraint!
    
    //宽度的约束对象
    @IBOutlet weak var pictureWidthCons: NSLayoutConstraint!
    
    //底部tabBar视图
    @IBOutlet weak var toolBar: UIStackView!
    
    //图片高度连线
    @IBOutlet weak var pictureTopCons: NSLayoutConstraint!
    
    //流水布局对象
    @IBOutlet weak var UICollectionViewFlowLayout: UICollectionViewFlowLayout!
    
    
    //视图模型
    var viewModel:LWHomeCellModel? {
        didSet{
            Label_time.text = viewModel?.timeText
            Label_content.text = viewModel?.statuses?.text
            Label_source.text = viewModel?.ScurceText
            Label_name.text = viewModel?.statuses?.user?.screen_name
            headIcon.sd_setImage(with: viewModel?.headURL)
            rankView.image = viewModel?.mbrankImage
            verifiedIcon.image = viewModel?.verifiedImage
            
           // let count = viewModel?.statuses?.pic_urls?.count ?? 0
            let count = viewModel?.imageType?.count ?? 0
            
            
            let PictuSize = calculatePictureViewSize(count: count)
            
            //跟新约束
            pictureHeightCons.constant = PictuSize.height
            pictureWidthCons.constant = PictuSize.width
            //根基是否有配图视图修改约束值
            pictureTopCons.constant = (count == 0 ? 0 : 8)
            
            //设置数据源
            //PictureView.pic_urls = viewModel?.statuses?.pic_urls
            PictureView.pic_urls = viewModel?.imageType
            
            lABEL_retweeteD?.text = viewModel?.statuses?.retweeted_status?.text
            
            
        }
    }
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        //设置自动换行
        Label_content.numberOfLines = 0
        Label_content.preferredMaxLayoutWidth = ScreenWidth - 2 * homeCellMargin
        
        //设置转发微博的自动换行,注意点,是一个隐式可选类型
        lABEL_retweeteD?.numberOfLines = 0
        lABEL_retweeteD?.preferredMaxLayoutWidth = ScreenWidth - 2 * homeCellMargin
        
        //图片显示的背景颜色
        PictureView.backgroundColor = UIColor.gray
        UICollectionViewFlowLayout.itemSize = CGSize(width: itemWith, height: itemWith)
        //设置间距
        UICollectionViewFlowLayout.minimumLineSpacing = PictureCellMargin
        UICollectionViewFlowLayout.minimumInteritemSpacing = PictureCellMargin
        
        //设置选中样式
        selectionStyle = .none
        
    }
    
    //根据配图数量计算配图视图的大小
    private func calculatePictureViewSize (count : Int) -> CGSize {
        //1. 没有视图
        if count == 0{
            return CGSize.zero
        }
        
        //2.单张视图
        //设置默认的itemSize
        UICollectionViewFlowLayout.itemSize = CGSize(width: itemWith, height: itemWith)
        if count == 1 {
            //获取单张图片的大小
            let url = URL(string: viewModel?.imageType?.last?.wap360 ?? "")
            //获取SD缓存图片
            let image = SDWebImageManager.shared().imageCache.imageFromDiskCache(forKey: url?.absoluteString)
            if let img = image {
                let size = img.size
                UICollectionViewFlowLayout.itemSize = size
                //配图视图大小
                return size
            }
            
            
            return CGSize(width: itemWith, height: itemWith)
        }
        //3. 四张视图
        if count == 4 {
            let width = itemWith + PictureCellMargin * 2.01
            return CGSize(width: width, height: width)
            
        }
        //4.其他多张视图
        let row = (count - 1) / 3 + 1
        
        //高度
        let h = itemWith * CGFloat(row) + PictureCellMargin * (CGFloat(row) - 1)
        
        return CGSize(width: MaxWidth, height: h)
        
        
    }

    
    
}
